package com.zsrd.self.base.config;

import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * 微信获取AccessToken并本地保存
 */
public class WxAccessToken {
    private static final long MAX_TIME = 7000;// 微信允许最长Access_token有效时间为7200秒,这里设置为7000秒

    public static String access_token_obj=null;
    /**
     * 获取Access_token 保存并且只保存2小时Access_token。如果超过两个小时重新获取；如果没有超过两个小时，直接获取
     * 思路:将获取到的Access_token和当前时间存储到file里，
     * 取出时判断当前时间和存储里面的记录的时间的时间差，如果大于MAX_TIME,重新获取，并且将获取到的存储到file替换原来的内容
     * 如果小于MAX_TIME，直接获取。
     */
    public static String getSavedAccessToken() throws IOException {
        Gson gson = new Gson();
        String mAccess_token = null;// 需要获取的Access_token;
        System.out.println(System.getProperty("user.di"));
        File file = new File("D:/weixin/weixinAccess_token.txt");// Access_token保存的位置
        // 如果文件不存在，创建
        if (!file.exists()) {
            file.getParentFile().mkdirs(); // 创建文件的父目录
            file.createNewFile();
        }
        if (file.length() == 0) {
            System.out.println("如果文件大小等于0，说明第一次使用，存入Access_token");
           // access_token_obj = WeixinUtil.getAccessToken();
            String token = WeixinUtil.getAccessToken();
            FileOutputStream fos = new FileOutputStream("D:/weixin/weixinAccess_token.txt", false);// 不允许追加
            AccessToken at = new AccessToken();
            at.setToken(token);
            at.setExpiresIn(Integer.valueOf(System.currentTimeMillis()/1000+""));
            System.out.println("at"+at);
            mAccess_token=token;
            String json = gson.toJson(at);
            fos.write(json.getBytes());
            fos.close();
        } else {
            System.out.println("读取文件内容");
            // 读取文件内容
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[2048];
            int len = fis.read(b);
            String mJsonAccess_token = new String(b, 0, len);// 读取到的文件内容
            System.out.println("读取到的文件内容"+mJsonAccess_token);
            AccessToken access_token = gson.fromJson(mJsonAccess_token, new AccessToken().getClass());
            if (access_token.getExpiresIn() != null) {
                System.out.println("access_token.getExpiresIn()"+access_token.getExpiresIn());

                long lastSaveTime = access_token.getExpiresIn();
                long nowTime = System.currentTimeMillis()/1000;
                long remianTime = nowTime - lastSaveTime;
             
                System.out.println("lastSaveTime"+lastSaveTime);
                System.out.println("nowTime"+nowTime);
                System.out.println("remianTime"+remianTime);
                System.out.println(remianTime < MAX_TIME);
                if (remianTime < MAX_TIME) {

                    AccessToken access = gson.fromJson(mJsonAccess_token, new AccessToken().getClass());
                    mAccess_token = access.getToken();
                    System.out.println("读取的mAccess_token"+mAccess_token);
                } else {
                    System.out.println("更新mJsonAccess_token");
                   // access_token_obj = WeiXinUtil.getToken();
                    FileOutputStream fos = new FileOutputStream(file, false);// 不允许追加
                    AccessToken newToken = new AccessToken();
                    String token=WeixinUtil.getAccessToken();
                    newToken.setToken(token);
                    newToken.setExpiresIn(Integer.valueOf(System.currentTimeMillis()/1000+""));
                    mAccess_token=token;
                    String json = gson.toJson(newToken);
                    fos.write((json).getBytes());
                    fos.close();
                }
            }
        }
        System.out.println("返回的mAccess_token"+mAccess_token);
        return mAccess_token;
    }

    /**
     * 获取JsApiTicket 保存并且只保存2小时JsApiTicket。如果超过两个小时重新获取；如果没有超过两个小时，直接获取
     * 思路:将获取到的JsApiTicket和当前时间存储到file里，
     * 取出时判断当前时间和存储里面的记录的时间的时间差，如果大于MAX_TIME,重新获取，并且将获取到的存储到file替换原来的内容
     * 如果小于MAX_TIME，直接获取。
     */
    public static String getSavedJsApiTicket(String accessToken) throws IOException {
        Gson gson = new Gson();
        String mJsApiTicket = null;// 需要获取的Access_token;
        System.out.println(System.getProperty("user.di"));
        File file = new File("D:/weixin/weixinJsApiTicket.txt");// Access_token保存的位置
        // 如果文件不存在，创建
        if (!file.exists())
            file.createNewFile();
        // 如果文件大小等于0，说明第一次使用，存入Access_token

        if (file.length() == 0) {
            System.out.println("如果文件大小等于0，说明第一次使用，存入JsApiTicket");
            // access_token_obj = WeixinUtil.getAccessToken();
            String aJsApiTicket = WeixinUtil.getJsApiTicket(accessToken);
            FileOutputStream fos = new FileOutputStream("D:/weixin/weixinJsApiTicket.txt", false);// 不允许追加
            AccessToken at = new AccessToken();
            at.setJsApiTicket(aJsApiTicket);
            at.setExpiresIn(Integer.valueOf(System.currentTimeMillis()/1000+""));
            System.out.println("at"+at);
            mJsApiTicket=aJsApiTicket;
            String json = gson.toJson(at);
            fos.write(json.getBytes());
            fos.close();
        } else {
            System.out.println("读取文件内容");
            // 读取文件内容
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[2048];
            int len = fis.read(b);
            String mJsonjsApiTicket = new String(b, 0, len);// 读取到的文件内容
            System.out.println("读取到的文件内容"+mJsonjsApiTicket);
            AccessToken jsApiTicket = gson.fromJson(mJsonjsApiTicket, new AccessToken().getClass());
            if (jsApiTicket.getExpiresIn() != null) {
                System.out.println("access_token.getExpiresIn()"+jsApiTicket.getExpiresIn());

                long lastSaveTime = jsApiTicket.getExpiresIn();
                long nowTime = System.currentTimeMillis()/1000;
                long remianTime = nowTime - lastSaveTime;

                System.out.println("lastSaveTime"+lastSaveTime);
                System.out.println("nowTime"+nowTime);
                System.out.println("remianTime"+remianTime);
                System.out.println(remianTime < MAX_TIME);
                if (remianTime < MAX_TIME) {

                    AccessToken access = gson.fromJson(mJsonjsApiTicket, new AccessToken().getClass());
                    mJsApiTicket = access.getJsApiTicket();
                    System.out.println("读取的JsApiTicket"+jsApiTicket);
                } else {
                    System.out.println("更新JsApiTicket");
                    // access_token_obj = WeiXinUtil.getToken();
                    FileOutputStream fos = new FileOutputStream(file, false);// 不允许追加
                    AccessToken newJsApiTicket = new AccessToken();
                    String jsapiTicket=WeixinUtil.getJsApiTicket(accessToken);
                    newJsApiTicket.setJsApiTicket(jsapiTicket);
                    newJsApiTicket.setExpiresIn(Integer.valueOf(System.currentTimeMillis()/1000+""));
                    mJsApiTicket=jsapiTicket;
                    String json = gson.toJson(newJsApiTicket);
                    fos.write((json).getBytes());
                    fos.close();
                }
            }
        }
        System.out.println("返回的JsApiTicket"+mJsApiTicket);
        return mJsApiTicket;
    }


}
